package com.qf.j2105.attendance.service.impl;

import com.qf.j2105.attendance.mapper.PunchingMapper;
import com.qf.j2105.attendance.service.PunchingService;
import com.qf.j2105.po.PunchingRecord;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PunchingServiceImpl implements PunchingService {
    @Resource
    private PunchingMapper punchingMapper;
    @Override
    public boolean addPuching(Date d,int eid) {
        int pday = new Date().getDay();
        int countPunching = punchingMapper.countPunching(eid, pday);
        Date noon = new Date(d.getYear() , d.getMonth(), d.getDay(), 12, 0, 0);
        Date afternoon = new Date(d.getYear(), d.getMonth(), d.getDay(), 13, 30, 00);
        PunchingRecord punchingRecord;

        if (countPunching < 2){
            switch (countPunching) {
                case 0:
                    if(noon.getTime()<=d.getTime()&&d.getTime()<afternoon.getTime()){
                        punchingRecord =
                                new PunchingRecord(null, eid, d.getYear() + 1900, d.getMonth() + 1,
                                        d.getDay(), 13, 30, 0, "on");
                    }else {
                        punchingRecord = new PunchingRecord(null, eid, d.getYear() + 1900, d.getMonth() + 1,
                                d.getDay(), d.getHours(), d.getMinutes(), d.getSeconds(), "on");
                    }
                    int i = punchingMapper.addPunching(punchingRecord);
                    if (i > 0) {
                        return true;
                    }
                    System.out.println("打卡失败1");
                    return false;
                case 1:
                    List<PunchingRecord> list = punchingMapper.findPunchingsByDate(eid, pday);
                    PunchingRecord r = list.get(0);
                    Date date = new Date(r.getPyear()-1900, r.getPmonth()-1, r.getPday(), r.getPhour(), r.getPminute(), r.getPsecond());
                    boolean flag = noon.getTime() <= date.getTime()&& date.getTime() <= afternoon.getTime();
                    Date date2 = new Date(d.getYear(),d.getMonth(),d.getDay(),d.getHours(),d.getMinutes(),d.getSeconds());
                    boolean flag2 = noon.getTime() <= date2.getTime()&& date2.getTime() <= afternoon.getTime();

                    if(!flag&flag2){
                        punchingRecord =
                                new PunchingRecord(null, eid, d.getYear() + 1900, d.getMonth() + 1,
                                        d.getDay(), 12, 0, 0, "off");
                        int j = punchingMapper.addPunching(punchingRecord);
                        if (j > 0) {
                            return true;
                        }
                        System.out.println("打卡失败2");
                        return false;
                    }
                    else if(flag && flag2){
//                        punchingRecord = new PunchingRecord(null, eid, d.getYear() + 1900, d.getMonth() + 1,
//                                d.getDay(), d.getHours(), d.getMinutes(), d.getSeconds(), "off");
                        System.out.println("打卡失败3");
                        return false;
                    }else {

                         punchingRecord = new PunchingRecord(null,eid,d.getYear()+1900,d.getMonth()+1,d.getDay(),
                                d.getHours(),d.getMinutes(),d.getSeconds(),"off");
                        int k = punchingMapper.addPunching(punchingRecord);
                        if (k>0){
                            return true;
                        }
                        System.out.println("打卡失败4");
                        return false;
                    }

            }
        }else {
            List<PunchingRecord> punchingList = punchingMapper.findPunchingsByDate(eid, pday);
            PunchingRecord p = punchingList.get(1);

            int rid = p.getRecordId();
            if (p.getPhour()==12&&p.getPminute()==0&&p.getPsecond()==0&&p.getPunchingType().equals("off")){
                System.out.println("打卡失败5");
                return false;
            }
             punchingRecord = new PunchingRecord(rid,eid,d.getYear()+1900,d.getMonth()+1,d.getDay(),
                    d.getHours(),d.getMinutes(),d.getSeconds(),"off");
            Date date = new Date(d.getYear(),d.getMonth(),d.getDay(),d.getHours(),d.getMinutes(),d.getSeconds());
            if(noon.getTime()<=date.getTime()&&date.getTime()<=afternoon.getTime()){
                punchingRecord =
                        new PunchingRecord(rid, eid, d.getYear() + 1900, d.getMonth() + 1,
                                d.getDay(), 12, 0, 0, "off");
            }
            int i = punchingMapper.updatePunching(punchingRecord);
            if (i>0){
                return true;
            }
            System.out.println("打卡失败6");
            return false;
        }
        return false;
    }

    @Override
    public List<PunchingRecord> findPunchingRecordByDay(int eid, Date date) {
        int pday = date.getDay();
        List<PunchingRecord> list = punchingMapper.findPunchingsByDate(eid, pday);
        return list;
    }
}
